package servlet;

import java.io.*;
import java.sql.*;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import util.*;
import bll.*;
import model.*;
import dal.*;

/**
 * Servlet implementation class CompanyServlet
 */
@WebServlet(urlPatterns = { "/Product", "/product", "/ProductServlet", "/productServlet" }, initParams = {
        @WebInitParam(name = "mode", value = "") })
public class ProductServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * Default constructor.
	 */
	public ProductServlet() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html; charset=UTF-8");

		String mode = request.getParameter("mode");

		if (mode.equalsIgnoreCase("new")) {

			if (RightUtil.<ProductModel>checkInsertRight()) {

				// TODO : MORE CODE HERE

                getServletContext().getRequestDispatcher("/productNew.jsp").forward(request, response);
			} else {
				// TODO no right
			}
		} else if (mode.equalsIgnoreCase("edit")) {

                ProductModel query = new ProductModel();				
                String id = request.getParameter("id");
			query.setId(Integer.parseInt(id));


				GenericDal<ProductModel, ProductModelExt> dal = new GenericDal<ProductModel, ProductModelExt>(
						new ProductModel());

				ProductModel obj = null;
				try {
					obj = dal.select(query);
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (ClassNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				if (obj != null) {

					// TODO : MORE CODE HERE

                    request.setAttribute("product", obj);
                    getServletContext().getRequestDispatcher("/productEdit.jsp").forward(request, response);

				} else {
					// TODO : data not found
				}			
		} else if (mode.equalsIgnoreCase("view")) {
			
                ProductModel query = new ProductModel();				
                String id = request.getParameter("id");
			query.setId(Integer.parseInt(id));


				GenericDal<ProductModel, ProductModelExt> dal = new GenericDal<ProductModel, ProductModelExt>(
						new ProductModel());

				ProductModel obj = null;
				try {
					obj = dal.select(query);
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (ClassNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				if (obj != null) {
					// TODO : LOAD MASTER DATA HERE
					// It's specific code that cannot be generated

                    request.setAttribute("product", obj);
                    getServletContext().getRequestDispatcher("/productView.jsp").forward(request, response);

				} else {
					// TODO : data not found
				}			
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html; charset=UTF-8");

		String mode = request.getParameter("mode");

		if (mode.equalsIgnoreCase("insert")) {
			
				ProductModel obj = new ProductModel();

                String short_name = request.getParameter("short_name");
					obj.setShortName(short_name);

String full_name = request.getParameter("full_name");
					obj.setFullName(full_name);

String foreign_name = request.getParameter("foreign_name");
					obj.setForeignName(foreign_name);

String code = request.getParameter("code");
					obj.setCode(code);

String description = request.getParameter("description");
					obj.setDescription(description);

String id_default_unit = request.getParameter("id_default_unit");
                    if (id_default_unit != null && !id_default_unit.isEmpty()) {
					    obj.setIdDefaultUnit(Integer.parseInt(id_default_unit));
                    }

String status = request.getParameter("status");
                    if (status != null && !status.isEmpty()) {
					    obj.setStatus(Integer.parseInt(status));
                    }

String is_draft = request.getParameter("is_draft");
					obj.setIsDraft(is_draft.equalsIgnoreCase("true")
							|| is_draft.equalsIgnoreCase("1"));

String notification = request.getParameter("notification");
                    if (notification != null && !notification.isEmpty()) {
					    obj.setNotification(Integer.parseInt(notification));
                    }



				

                obj.setCreator(SessionUtil.getCurrentUserID());

				if (CommonBll.checkInputs(obj)) {
					GenericDal<ProductModel, ProductModelExt> dal = new GenericDal<ProductModel, ProductModelExt>(
							new ProductModel());

					int res = 0;
					try {
						res = dal.insert(obj);
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (ClassNotFoundException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}

					if (res > 0) {
						// TODO : success, redirect to somewhere
					} else {
						// TODO : processing error here
					}
				} else {

					// invalid input

                    request.setAttribute("product", obj);
                    getServletContext().getRequestDispatcher("/productInput.jsp").forward(request, response);

				}		
		} else if (mode.equalsIgnoreCase("update")) {		

                ProductModel query = new ProductModel();				
                String id = request.getParameter("id");
			query.setId(Integer.parseInt(id));


				GenericDal<ProductModel, ProductModelExt> dal = new GenericDal<ProductModel, ProductModelExt>(
						new ProductModel());

				ProductModel obj = null;
				try {
					obj = dal.select(query);
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (ClassNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				if (obj != null) {

                    String short_name = request.getParameter("short_name");
					obj.setShortName(short_name);

String full_name = request.getParameter("full_name");
					obj.setFullName(full_name);

String foreign_name = request.getParameter("foreign_name");
					obj.setForeignName(foreign_name);

String code = request.getParameter("code");
					obj.setCode(code);

String description = request.getParameter("description");
					obj.setDescription(description);


					String id_default_unit = request.getParameter("id_default_unit");
					obj.setIdDefaultUnit(Integer.parseInt(id_default_unit));


					String status = request.getParameter("status");
					obj.setStatus(Integer.parseInt(status));

String is_draft = request.getParameter("is_draft");
					obj.setIsDraft(is_draft.equalsIgnoreCase("true")
							|| is_draft.equalsIgnoreCase("1"));


					String notification = request.getParameter("notification");
					obj.setNotification(Integer.parseInt(notification));



				

                    if (CommonBll.checkInputs(obj)) {	

					    int res = Integer.MIN_VALUE;
					    try {
						    res = dal.update(obj);
					    } catch (SQLException e) {
						    // TODO Auto-generated catch block
						    e.printStackTrace();
					    } catch (ClassNotFoundException e) {
						    // TODO Auto-generated catch block
						    e.printStackTrace();
					    }

					    if (res == 0) {
						    // TODO success
					    } else {
						    // TODO error
					    }
                    } else {
					request.setAttribute("product", obj);
					getServletContext().getRequestDispatcher("/productEdit.jsp").forward(request, response);
				    }
				} else {
					// TODO data not found
				}			
		} else if (mode.equalsIgnoreCase("delete")) {		

                ProductModel query = new ProductModel();				
                String id = request.getParameter("id");
			query.setId(Integer.parseInt(id));


				GenericDal<ProductModel, ProductModelExt> dal = new GenericDal<ProductModel, ProductModelExt>(
						new ProductModel());

				int res = Integer.MIN_VALUE;
				try {
					res = dal.delete(query);
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (ClassNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				if (res == 0) {
					// TODO success
				} else {
					// TODO fail
				}			
		}
	}

}